//
//  TTURLSessionManager.m
//  01-AFN
//
//  Created by peironggao on 2017/10/14.
//  Copyright © 2017年 gaopeirong. All rights reserved.
//

#import "TTURLSessionManager.h"
#import "AFNetworking.h"

@interface TTURLSessionManager()

@property (nonatomic, strong) AFHTTPSessionManager *jsonSessionManager;

@property (nonatomic, strong) AFHTTPSessionManager *httpSessionManager;

@end

@implementation TTURLSessionManager

+ (instancetype)shareInstance
{
    static TTURLSessionManager *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    if (self = [super init])
    {
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
//        config.HTTPAdditionalHeaders
//        config.timeoutIntervalForRequest
        
        _jsonSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
        AFJSONResponseSerializer *responseSerializer = [[AFJSONResponseSerializer alloc] init];
        _jsonSessionManager.responseSerializer = responseSerializer;
        
        _httpSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
        _httpSessionManager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
        

    }
    return self;
}

- (NSURLSessionDataTask *)getRequestWithURLString:(NSString *)urlString
                         params:(NSDictionary *)params
                        success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                        failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    return [_jsonSessionManager GET:urlString parameters:params success:success failure:failure];
}

- (NSURLSessionDataTask *)postRequestWithURLString:(NSString *)urlString
                          params:(NSDictionary *)params
                         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                         failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    return [_jsonSessionManager POST:urlString parameters:params success:success failure:failure];
}

- (NSURLSessionDataTask *)getHttpResponseRequestWithURLString:(NSString *)urlString
                                                       params:(NSDictionary *)params
                                                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                                                      failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    return [_httpSessionManager GET:urlString parameters:params success:success failure:failure];
}

- (NSURLSessionDataTask *)postHttpResponseRequestWithURLString:(NSString *)urlString
                                            params:(NSDictionary *)params
                                           success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                                           failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    return [_httpSessionManager POST:urlString parameters:params success:success failure:failure];
}

@end
